package com.an.vhr.controller.system.basic;

import com.an.vhr.bean.Department;
import com.an.vhr.bean.RespBean;
import com.an.vhr.service.DepartmentService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: an1089
 **/

@RestController
@RequestMapping("/system/basic/department")
public class DepartmentController {
    @Resource
    DepartmentService departmentService;

    @GetMapping("/")
    public List<Department> getAllDepartments(){
        return departmentService.getAllDepartmentsByParentId(-1);
    }

    @PostMapping("/")
    public RespBean addDep(@RequestBody Department department){
        departmentService.addDep(department);
        if(department.getResult() == 1){
            return RespBean.ok("添加成功",department);
        }
        return RespBean.error("添加失败");
    }

    @DeleteMapping("/{id}")
    public RespBean deleteDepById(@PathVariable Integer id){
        Department department = new Department();
        department.setId(id);
        departmentService.deleteDepById(department);
        if(department.getResult() == -2){
            return RespBean.error("该部门下还有子部门，删除失败");
        }else if(department.getResult() == -1){
            return RespBean.error("该部门下还有员工，删除失败");
        }else if(department.getResult() == 1){
            return RespBean.ok("删除成功");
        }
        return RespBean.error("删除失败");

    }
}
